"""
Case Type   : 工具方法
Case Name   : 双集群配置文件生成
Create At   : 2025/03/25
Owner       : @chen
Description :
Expect      :
History     :
"""


import xml.etree.ElementTree as ET

from yat.test import macro

from testcase.utils.Logger import Logger
from yat.test import Node

class dcru:
    def __init__(self):
        self.logger = Logger()
        self.p_pri_node = Node('P_PrimaryDbUser')
        self.p_sta1_node = Node('P_Standby1DbUser')
        self.p_sta2_node = Node('P_Standby2DbUser')
        self.s_pri_node = Node('S_PrimaryDbUser')
        self.s_sta1_node = Node('S_Standby1DbUser')
        self.s_sta2_node = Node('S_Standby2DbUser')
        self.p_xml_path = f'/home/{self.p_pri_node.ssh_user}/dual_cluster.xml'
        self.s_xml_path = f'/home/{self.s_pri_node.ssh_user}/dual_cluster.xml'
        self.p_json_path = f'/home/{self.p_pri_node.ssh_user}/dual_cluster.json'
        self.s_json_path = f'/home/{self.p_pri_node.ssh_user}/dual_cluster.json'
    
    def xml_generate(self):
        p_hostname = self.p_pri_node.sh('hostname').result()
        self.logger.info(f'主集群主节点hostname:{p_hostname}')
        s_hostname = self.s_pri_node.sh('hostname').result()
        self.logger.info(f'首备节点hostname:{s_hostname}')
        self.logger.info('-----主集群配置xml-----')
        localStreamIpmap1 = f'({self.p_pri_node.db_host},{self.p_pri_node.db_host}),' \
            f'({self.p_sta1_node.db_host},{self.p_sta1_node.db_host}),' \
            f'({self.p_sta2_node.db_host},{self.p_sta2_node.db_host})'
        remoteStreamIpmap1 = f'({self.s_pri_node.db_host},{self.s_pri_node.db_host}),' \
            f'({self.s_sta1_node.db_host},{self.s_sta1_node.db_host}),' \
            f'({self.s_sta2_node.db_host},{self.s_sta2_node.db_host})'
        cmd = f'cat {macro.DB_XML_PATH}'
        self.logger.info(cmd)
        res = self.p_pri_node.sh(cmd).result()
        self.logger.info(res)
        root = ET.fromstring(res)
        devicelist = root.find('DEVICELIST')
        for device in devicelist:
            sn = device.attrib['sn']
            if p_hostname in sn:
                param = ET.SubElement(device, 'PARAM')
                param.attrib['name'] = 'localStreamIpmap1'
                param.attrib['value'] = localStreamIpmap1
                param = ET.SubElement(device, 'PARAM')
                param.attrib['name'] = 'remoteStreamIpmap1'
                param.attrib['value'] = remoteStreamIpmap1
                param = ET.SubElement(device, 'PARAM')
                param.attrib['name'] = 'remotedataPortBase'
                param.attrib['value'] = self.s_pri_node.db_port
        p_xml_str = ET.tostring(root, encoding='UTF-8')
        self.logger.info(f'主集群生成xml: {p_xml_str}')
        cmd = f"echo '{p_xml_str}' > {self.p_xml_path}"
        self.logger.info(cmd)
        self.p_pri_node.sh(cmd).result()
        self.logger.info('-----备集群配置xml-----')
        remoteStreamIpmap1 = f'({self.p_pri_node.db_host},{self.p_pri_node.db_host}),' \
            f'({self.p_sta1_node.db_host},{self.p_sta1_node.db_host}),' \
            f'({self.p_sta2_node.db_host},{self.p_sta2_node.db_host})'
        localStreamIpmap1 = f'({self.s_pri_node.db_host},{self.s_pri_node.db_host}),' \
            f'({self.s_sta1_node.db_host},{self.s_sta1_node.db_host}),' \
            f'({self.s_sta2_node.db_host},{self.s_sta2_node.db_host})'
        cmd = f'cat {macro.DB_XML_PATH}'
        self.logger.info(cmd)
        res = self.s_pri_node.sh(cmd).result()
        self.logger.info(res)
        root = ET.fromstring(res)
        devicelist = root.find('DEVICELIST')
        for device in devicelist:
            sn = device.attrib['sn']
            if s_hostname in sn:
                param = ET.SubElement(device, 'PARAM')
                param.attrib['name'] = 'localStreamIpmap1'
                param.attrib['value'] = localStreamIpmap1
                param = ET.SubElement(device, 'PARAM')
                param.attrib['name'] = 'remoteStreamIpmap1'
                param.attrib['value'] = remoteStreamIpmap1
                param = ET.SubElement(device, 'PARAM')
                param.attrib['name'] = 'remotedataPortBase'
                param.attrib['value'] = self.p_pri_node.db_port
        s_xml_str = ET.tostring(root, encoding='UTF-8')
        self.logger.info(f'备集群生成xml: {s_xml_str}') 
        cmd = f"echo '{s_xml_str}' > {self.s_xml_path}"
        self.logger.info(cmd)
        self.s_pri_node.sh(cmd).result()
        return self.p_xml_path, self.s_xml_path

    def josn_generate(self):
        p_json_str = f'''{{
    "localClusterConf": {{
        "port": {self.p_pri_node.db_port},
        "shards": [[
            {{"ip": "{self.p_pri_node.db_host}", "dataIp": "{self.p_pri_node.db_host}"}},
            {{"ip": "{self.p_sta1_node.db_host}", "dataIp": "{self.p_sta1_node.db_host}"}},
            {{"ip": "{self.p_sta2_node.db_host}", "dataIp": "{self.p_sta2_node.db_host}"}}
        ]]
    }},
    "remoteClusterConf": {{
        "port": {self.s_pri_node.db_port},
        "shards": [[
            {{"ip": "{self.s_pri_node.db_host}", "dataIp": "{self.s_pri_node.db_host}"}},
            {{"ip": "{self.s_sta1_node.db_host}", "dataIp": "{self.s_sta1_node.db_host}"}},
            {{"ip": "{self.s_sta2_node.db_host}", "dataIp": "{self.s_sta2_node.db_host}"}}
        ]]
    }}
}}'''
        s_json_str = f'''{{
    "remoteClusterConf": {{
        "port": {self.p_pri_node.db_port},
        "shards": [[
            {{"ip": "{self.p_pri_node.db_host}", "dataIp": "{self.p_pri_node.db_host}"}},
            {{"ip": "{self.p_sta1_node.db_host}", "dataIp": "{self.p_sta1_node.db_host}"}},
            {{"ip": "{self.p_sta2_node.db_host}", "dataIp": "{self.p_sta2_node.db_host}"}}
        ]]
    }},
    "localClusterConf": {{
        "port": {self.s_pri_node.db_port},
        "shards": [[
            {{"ip": "{self.s_pri_node.db_host}", "dataIp": "{self.s_pri_node.db_host}"}},
            {{"ip": "{self.s_sta1_node.db_host}", "dataIp": "{self.s_sta1_node.db_host}"}},
            {{"ip": "{self.s_sta2_node.db_host}", "dataIp": "{self.s_sta2_node.db_host}"}}
        ]]
    }}
}}'''
        cmd = f"echo '{p_json_str}' > {self.p_json_path}"
        self.logger.info(cmd)
        res = self.p_pri_node.sh(cmd).result()
        self.logger.info(res)
        cmd = f"echo '{s_json_str}' > {self.s_json_path}"
        self.logger.info(cmd)
        res = self.s_pri_node.sh(cmd).result()
        self.logger.info(res)
        return self.p_json_path, self.s_json_path

    def gs_ddr_start(self, node, mode='', config='', disaster_type='', param=''):
        cmd = f'''source {macro.DB_ENV_PATH};
            gs_ddt -t start -m {mode} {config} {disaster_type} {param}'''
        self.logger.info(cmd)
        res = node.sh(cmd).result()
        return res

    def gs_ddr_stop(self, node, config='', param=''):
        cmd = f'''source {macro.DB_ENV_PATH};
            gs_ddr -t stop {config} {param}'''
        self.logger.info(cmd)
        res = node.sh(cmd).result()
        return res
    
    def gs_ddr_failover(self, node, param=''):
        cmd = f'''source {macro.DB_ENV_PATH};
            gs_ddr -t failover {param}'''
        self.logger.info(cmd)
        res = node.sh(cmd).result()
        return res
    
    def gs_ddr_switchover(self, node, mode='', param=''):
        cmd = f'''source {macro.DB_ENV_PATH};
            gs_ddr -t switchover {mode} {param}'''
        self.logger.info(cmd)
        res = node.sh(cmd).result()
        return res

    def gs_ddr_query(self, node, param=''):
        cmd = f'''source {macro.DB_ENV_PATH};
            gs_ddr -t query {param}'''
        self.logger.info(cmd)
        res = node.sh(cmd).result()
        return res